<?php
    /**
    * Demand to be emailed Management
    * @author: trungdt
    * @since: mar 10, 2011
    * @version: 1.0
    * 
    *Change logs
    */
    if (!defined('EXPONENT')) exit('');

    /**
    * How many items per page
    * WARNING: Must implement better in module config
    */
    $max_item =15;
    // fix bug router
    if (isset($_REQUEST['id']))
        $_REQUEST['page']=$_REQUEST['id'];
    else
        $_REQUEST['page']=1;

    // check permission
    $loc=null;
    $loc->mod="demandmodule";

    // Administrator access restrict
    if (exponent_users_isLoggedIn() && (
        exponent_permissions_check("manage",$loc) 
        || exponent_permissions_check("approve_demand",$loc)
    ))
    {
        $lang=exponent_lang_loadFile("modules/demandmodule/actions/demand-manage.php");
        $lang2=exponent_lang_loadFile("modules/demandmodule/demand.php");

        // set flow
        exponent_flow_set(SYS_FLOW_PROTECTED,SYS_FLOW_ACTION);

        // pagination
        // we count all post
        $count_post = $db->countObjects('demands',"sendmail=1")+$db->countObjects('witness',"sendmail=1");

        // page split
        // cal how many posts
        $pages=floor($count_post / $max_item);
        if ($count_post % $max_item != 0)
            $pages++;

        // get page
        $page=1;
        if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
        if (($page<1) || ($page>$pages)) $page=1;
        $start_pos=($page-1)*$max_item;
        // end of pagination
        // add field sendmail, 
        $fields_demand="{$db->prefix}demands.id, {$db->prefix}demands.name, {$db->prefix}demands.content, {$db->prefix}demands.user_id, {$db->prefix}demands.username, {$db->prefix}demands.post_date,
        {$db->prefix}demands.`status`, {$db->prefix}demands.unapprove_reason, {$db->prefix}demands.unapprove_username, {$db->prefix}demands.publish_date, {$db->prefix}demands.important_level, 1 `type`, {$db->prefix}subjects.name subject_name, sendmail , pray_count";

        $where=" sendmail=1";     
        $data=$db->selectObjectsBySql("
        SELECT {$fields_demand} 
        FROM 
        {$db->prefix}demands INNER JOIN {$db->prefix}subjects
        ON {$db->prefix}demands.subject_id = {$db->prefix}subjects.id
        WHERE ({$where})

        ORDER BY status ASC, post_date DESC, important_level DESC, publish_date DESC LIMIT {$start_pos},{$max_item}
        ");

        $show_unapprove=false;
        if (exponent_users_isLoggedIn() && 
        (exponent_permissions_check("administrate",$loc) || 
        exponent_permissions_check("approve_demand",$loc))
        )
        {
            $show_unapprove=true;
        }
        demandmodule::demand_witness_show($data, $lang, $lang2, $show_unapprove);

        $title=$lang2['demand_email'];
        $page_action="demand_email";

        $template=new template("demandmodule","_demand_manage");
        $template->assign("data",$data);
        $template->register_permissions(array('approve_demand',"administrate"),$loc);  
        $template->assign("page",$page);
        $template->assign("pages",$pages);
        $template->assign("title",$title);
        $template->assign("page_action",$page_action); 
        $template->assign("start_index",$start_pos);
        $template->assign("demand_manage",1);
        if (exponent_users_isLoggedIn())
        {
            $template->assign("logged",1);
        }
        _ab_theme_action_register_title($title." - ".SITE_TITLE);
        if (exponent_users_isLoggedIn())
        {
            $template->assign("logged",1);
        }
        $template->output();
    }
    else if (aboid_isLoggedin()) // just logged by openid, not by real account
        {
            flash("", $ilang['openid_not_allow']);
            $tlink=exponent_core_makeLink(array(
            'module'=>'loginmodule',
            'action'=>'createuser'));
            header("Location: {$tlink}");
        }
        else // not login? dont allow to access
        {
            $tlink=exponent_core_makeLink(array(
            'module'=>'loginmodule',
            'action'=>'loginredirect'));
            header("Location: {$tlink}");
    }
?>
